home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / recio202.zip / testcop.c < prev    next >
C/C++ Source or Header  |  1994-05-05  |  8KB  |  249 lines

  1. /* testcop.c - tests column delimited put functions */
  2. /* recio version 2.02, release May 5, 1994 */
  3. /* Copyright (C) 1994 William Pierpoint */
  4.  
  5. #include <ctype.h>
  6. #include <errno.h>
  7. #include <limits.h>
  8. #include <float.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <io.h>
  13.  
  14. #include "recio.h"
  15.  
  16. /* errors to stderr */
  17. #define errout  stdout
  18.  
  19. /****************************************************************************
  20. Dynamic string concat function, kludged for use with recio.  This 
  21. function is not part of recio nor was rseterr() designed to work 
  22. with it, but you get the idea. 
  23. Precondition: set dst to null ptr when declaring it, e.g. char *dst=NULL;
  24. Syntax: the assignment operator is mandatory, i.e. dst=scats(dst, src);
  25. Obligation: free dst when finished with it.
  26. *****************************************************************************/
  27. char *                  /* return dst                                       */
  28.     scats(              /* concat string dynamically                        */
  29.         char *dst,      /* destination string pointer                       */
  30.         char *src)      /* source string pointer                            */
  31. /****************************************************************************/
  32. {                       
  33.   size_t dlen;          /* strlen of dst */
  34.   size_t slen;          /* strlen of src */
  35.  
  36.   /* if null src pointer or src empty, do nothing */
  37.   if (!src || !*src) {
  38.     goto done;
  39.   }
  40.   
  41.   if (dst) {
  42.     dlen = strlen(dst);
  43.     slen = strlen(src);
  44.     do {
  45.       dst = (char *) realloc(dst, dlen+slen+1);
  46.       if (!dst) {
  47.         if (rseterr(NULL, ENOMEM)) goto done;
  48.       }
  49.     } while (!dst);
  50.     strcat(dst, src);
  51.   } else {
  52.      do {
  53.        dst = strdup(src);
  54.        if (!dst) {
  55.          if (rseterr(NULL, ENOMEM)) goto done;
  56.        }
  57.      } while (!dst);
  58.    }
  59. done:
  60.   return dst;
  61. }
  62.  
  63. enum {RESET, INCR, REPORT};
  64. /****************************************************************************/
  65. void                         /* return nothing                              */
  66.     warnttl(                 /* warning totals                              */
  67.         int action,          /* action (0=reset, 1=increment, 2=report)     */
  68.         int warnum)          /* warning number                              */
  69. /****************************************************************************/
  70. {
  71.   static unsigned int widthcnt=0;
  72.   static unsigned int noregcnt=0;
  73.  
  74.   switch (action) {
  75.   case RESET:
  76.     widthcnt=0;
  77.     return;
  78.   case INCR:
  79.     switch(warnum) {
  80.     case R_WWIDTH: 
  81.       widthcnt++; 
  82.       return;
  83.     case R_WNOREG: 
  84.       noregcnt++; 
  85.       return;
  86.     }
  87.     return;
  88.   case REPORT:
  89.     fprintf(errout, "\n");
  90.     if (widthcnt) 
  91.       fprintf(errout, "WARNING -- %s -- %u times\n", 
  92.        rstrwarning(R_WWIDTH), widthcnt);
  93.     if (noregcnt) 
  94.       fprintf(errout, "WARNING -- %s\n", rstrwarning(R_WNOREG));
  95.     return;
  96.   }
  97. }
  98.  
  99. /****************************************************************************/
  100. void                         /* returns nothing                             */
  101.     rwarnfn(                 /* recio callback warning function             */
  102.         REC *rp)             /* record pointer                              */
  103. /****************************************************************************/
  104. {
  105.   if (risvalid(rp)) warnttl(INCR, rwarning(rp));
  106. }
  107.  
  108. /****************************************************************************/
  109. void                         /* returns nothing                             */
  110.     rerrfn(                  /* recio callback error function               */
  111.         REC *rp)             /* record pointer                              */
  112. /****************************************************************************/
  113. {
  114.   if (risvalid(rp)) {
  115.   
  116.     /* determine cause of error */
  117.     switch (rerror(rp)) {
  118.  
  119.     case R_ENOPUT:   /* could not write data */
  120.       fprintf(errout, "ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
  121.       break;
  122.  
  123.     /* fatal errors (R_EINVMOD, R_EINVAL, R_ENOMEM) */
  124.     default:
  125.       fprintf(errout, "FATAL ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
  126.       abort();
  127.       break;
  128.     }
  129.   
  130.   /* invalid record pointer */
  131.   } else {
  132.     switch (errno) {
  133.  
  134.     /* non-fatal errors */
  135.     case EACCES:
  136.     case EMFILE:
  137.       fprintf(errout, "WARNING: %s\n", strerror(errno));
  138.       break;
  139.  
  140.     /* fatal errors (EINVAL, ENOMEM) */
  141.     default:
  142.       fprintf(errout, "FATAL ERROR: %s\n", strerror(errno));
  143.       abort();
  144.       break;
  145.     }
  146.   }
  147. }
  148.  
  149. /****************************************************************************/
  150. void putcolnumbers(void)
  151. /****************************************************************************/
  152. {
  153. puts("");
  154. puts("         1         2         3         4         5         6");
  155. puts("123456789012345678901234567890123456789012345678901234567890");
  156. }
  157.  
  158. /****************************************************************************
  159. main
  160. *****************************************************************************/
  161. int main()
  162. {
  163.   int j;                        /* loop index */
  164.   int i;                        /* integer field */
  165.   unsigned int ui;              /* unsigned integer field */
  166.   long l;                       /* long field */
  167.   unsigned long ul;             /* unsigned long field */
  168.   float f;                      /* float field */
  169.   double d;                     /* double field */
  170.   int ch;                       /* character field */
  171.   char *str = NULL;             /* string field */
  172.   char str1[] = "A";            /* string consisting of one letter */
  173.   
  174.   /* install warning and error functions */
  175.   rsetwarnfn(rwarnfn);
  176.   rseterrfn(rerrfn);
  177.   
  178.   /* set beginning column number to 1 */
  179.   rsetbegcolno(recout, 1);
  180.   
  181.   /* if output not redirected */
  182.   if (isatty(fileno(stdout))) {
  183.     /* print instructions */
  184.     puts("TESTCOP version 2.02 Copyright (C) 1994 William Pierpoint");
  185.     puts("Tests recio column delimited put functions.\n");
  186.     puts("Field type           Columns");
  187.     puts("----------------    ---------");
  188.     puts("Integer............  1 to  5");
  189.     puts("Unsigned Integer...  6 to 10");
  190.     puts("Long............... 11 to 20");
  191.     puts("Unsigned Long...... 21 to 30");
  192.     puts("Float.............. 31 to 40");
  193.     puts("Double............. 41 to 50");
  194.     puts("Character..........    51   ");
  195.     puts("String............. 52 to 60");
  196.   }
  197.  
  198.   /* initialize data */
  199.   i = -1;
  200.   ui = 1;
  201.   l = -1L;
  202.   ul = 1L;
  203.   f = 1.111111;
  204.   d = 1.111111111111111111111;
  205.   ch = 'a';
  206.   str = scats(str, str1);
  207.  
  208.   /* loop through data */
  209.   for (j=0; j<11; j++) {
  210.  
  211.     putcolnumbers();
  212.  
  213.     rcputi(recout,   1,  5, i);
  214.     rcputui(recout,  6, 10, ui);
  215.     rcputl(recout,  11, 20, l);
  216.     rcputul(recout, 21, 30, ul);
  217.     rcputf(recout,  31, 40, f);
  218.     rcputd(recout,  41, 50, d);
  219.     rcputc(recout,  51, ch);
  220.     rcputs(recout, 52, 60, str);
  221.  
  222.     i  *= 10;
  223.     ui *= 10;
  224.     l  *= 10L;
  225.     ul *= 10L;
  226.     f  *= 10.;
  227.     d  *= 10.;
  228.     ch += 1;
  229.     *str1 = toupper(ch);
  230.     str = scats(str, str1);
  231.  
  232.     rputrec(recout);
  233.   }
  234.   
  235.   /* free string fields */
  236.   free (str);
  237.   
  238.   /* check stream for warnings */
  239.   if (rwarning(recout)) warnttl(REPORT, NULL);
  240.  
  241.   /* check stream for error */
  242.   if (rerror(recout)) { 
  243.     fprintf(stdout, "\nERROR %s -- %s\n", 
  244.      rnames(recout), rerrstr(recout));
  245.     exit (EXIT_FAILURE);
  246.   }
  247.   return EXIT_SUCCESS;
  248. }
  249.